package com.company.common.auth.filter;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.MimeHeaders;

import com.alibaba.fastjson.JSON;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.cache.CacheService;
import com.company.cloud.common.exception.BusinessException;
import com.company.common.auth.AuthSecurityManager;
import com.company.common.auth.constant.AuthConstant;

import lombok.extern.log4j.Log4j2;

@Log4j2
public class AuthorizationFilter implements Filter {

    private CacheService cacheService;

    public AuthorizationFilter(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String token = httpRequest.getHeader("token");
        String lang = httpRequest.getHeader("lang");
        String url = httpRequest.getServletPath();
        LoginUserInfo user = null;
        try {
            user = AuthSecurityManager.check(url, token, this.cacheService);
            if (user != null) {
                request.setAttribute(AuthConstant.REQ_USER_KEY, user);
            }
            chain.doFilter(request, response);
        } catch (BusinessException e) {
            httpResponse.setStatus(401);
            httpResponse.setContentType("application/json;charset=UTF-8");
            httpResponse.setCharacterEncoding("utf-8");
            BaseResult result = BaseResult.error(e.getCode(), e.getMessage());
            httpResponse.getWriter().write(JSON.toJSONString(result));
        }
    }


    private void reflectSetparam(HttpServletRequest request, String key, String value) {
        Class<? extends HttpServletRequest> requestClass = request.getClass();
        try {
            Field request1 = requestClass.getDeclaredField("request");
            request1.setAccessible(true);
            Object o = request1.get(request);
            Field coyoteRequest = o.getClass().getDeclaredField("coyoteRequest");
            coyoteRequest.setAccessible(true);
            Object o1 = coyoteRequest.get(o);
            Field headers = o1.getClass().getDeclaredField("headers");
            headers.setAccessible(true);
            MimeHeaders o2 = (MimeHeaders) headers.get(o1);
            o2.addValue(key).setString(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void destroy() {
    }
}
